package com.mantru.iotpot.Cook.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mantru.iotpot.DateClass.CookStep;
import com.mantru.iotpot.R;

import java.util.List;

public class CookStepListAdapter extends BaseAdapter {

    private List<CookStep> cookSteps; // CookStep 是一个数据模型类

    public CookStepListAdapter(List<CookStep> cookSteps) {
        this.cookSteps = cookSteps;
    }

    @Override
    public int getCount() {
        return cookSteps.size();
    }

    @Override
    public Object getItem(int position) {
        return cookSteps.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        CookStep cookStep = cookSteps.get(position);
        int x = -1;
        if (cookStep.getSteep_status().equals("0")){//等待
            holder.cookStep.setText("等待中");//状态
            holder.cookMaterial.setImageResource(R.drawable.wait);
        } else if (cookStep.getSteep_status().equals("1")) {//执行
            holder.cookStep.setText("进行中");//状态
            holder.cookMaterial.setImageResource(R.drawable.doing);
        } else if (cookStep.getSteep_status().equals("2")) {//完成
            holder.cookStep.setText("已完成");//状态
            holder.cookMaterial.setImageResource(R.drawable.finish);
            x = 0;
        }
        holder.cookname.setText(cookStep.getSteep_name() + "");
        if (x != 0){
            holder.cookMaterialSecond.setText(cookStep.getRemain_time() + "s");
        }else {
            holder.cookMaterialSecond.setVisibility(View.GONE);
        }
        // 判断是否是最后一项
        // 判断是否是最后一项
        if (position == cookSteps.size() - 1) {
            holder.cookXian.setVisibility(View.GONE); // 隐藏 cook_xian ImageView
        } else {
            holder.cookXian.setVisibility(View.VISIBLE); // 显示 cook_xian ImageView
        }

        return convertView;
    }

    private static class ViewHolder {
        public TextView cookStep;
        public ImageView cookMaterial;
        public TextView cookname;
        public TextView cookMaterialSecond;
        public ImageView cookXian;
        public ViewHolder(View view) {
            cookStep = view.findViewById(R.id.cook_step1);
            cookMaterial = view.findViewById(R.id.cook_finish1);
            cookname = view.findViewById(R.id.cook_material1);
            cookMaterialSecond = view.findViewById(R.id.cook_material_secound1);
            cookXian = view.findViewById(R.id.cook_xian);
        }
    }
}